package WebRoot.demo4.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.hoyi.DB.CLuster.DataBaseCluster;
import org.hoyi.DB.Interface.IDatabase;
import org.hoyi.dishop.Hoyipage;
import org.hoyi.district.DistrictCtrls;
import org.hoyi.district.disctrictType;
import org.hoyi.wb.comment.RequestMode;
import org.hoyi.wb.comment.RequestType;

import WebRoot.demo4.entity.orders;

@RequestMode(MODE = { RequestType.GET, RequestType.POST })
public class ClusterDemo extends Hoyipage {

	public void LOADDEF() {
		int ret = DistrictCtrls.Load_DEF_ENTS("orders", orders.class, disctrictType.BALANCE_LOAD);
		this.HandlerEx("load success!~", ret);
	}

	// 增加订单
	public void addOrder() {
		IDatabase database = DistrictCtrls.get_Insert_DB("orders", orders.class);
		orders _order = new orders(database.Get_Clust_UUID(), "1", "now", "note");

		int result = _order.Insert(database);
		this.HandlerEx("添加订单", result);
	}

	// 查找订单
	public void findOrder() {
		String uxid = this.getParams("uxid");
		String cluster_key = DistrictCtrls.GetClustKey(uxid);
		IDatabase databs = DataBaseCluster.Get_READ_SERVER("orders", cluster_key);

		// DataBaseCluster.clusters.get("orders").keySet();
		// DistrictCtrls.Get_ENT_COUNT_KEY("orders", "c1", orders.class);

		orders _orders = orders.E().Where(orders.uxid.Equals(uxid)).First(databs);

		// List<orders> list = orders.E().Where(orders.uxid.Equals(uxid))
		// .Select(databs);
		// String sql = "select * from orders where uxid=?";
		// List<DbParameter> params = new ArrayList<DbParameter>();
		// params.add(new DbParameter("uxid", uxid));
		// List<orders> list2 = databs.ExecuteTableEx(sql, params).toEntitys(
		// orders.class);
		// System.out.println("list1=" + list);
		// System.out.println("list2=" + list2);
		this.WriteUTF8JSONDATAMSG(1, "success", _orders);

	}

	/**
	 * 查詢數量
	 */
	public void SelectNum() {
		String note = this.getParams("note");
		// int pagesize = 2;
		// String pgidx = this.getParams("pgidx");
		long startidx = Integer.parseInt(this.getParams("startidx"));
		long endidx = Integer.parseInt(this.getParams("endidx"));

		// 每个区的满足条件的条数.
		List<Clus_count> clus_counts2 = new ArrayList<>();

		for (String clus_keys : DataBaseCluster.clusters.get("orders").keySet()) {
			IDatabase _tdb = DataBaseCluster.Get_READ_SERVER("orders", clus_keys);
			long l = orders.E().Where(orders.note.Equals(note)).LongCount(_tdb);
			clus_counts2.add(new Clus_count(clus_keys, l));
		}

		Map<String, Map<String, Long>> map = ClusHandle.LimitCount(startidx, endidx, clus_counts2);
		this.WriteUTF8JSONDATAMSG(1, "msg", map);
	}

	/**
	 * 查詢數據
	 */
	public void SelectResult() {
		String note = this.getParams("note");
		// int pagesize = 2;
		// String pgidx = this.getParams("pgidx");
		long startidx = Integer.parseInt(this.getParams("startidx"));
		long endidx = Integer.parseInt(this.getParams("endidx"));

		// 每个区的满足条件的条数.
		List<Clus_count> clus_counts2 = new ArrayList<>();

		for (String clus_keys : DataBaseCluster.clusters.get("orders").keySet()) {
			IDatabase _tdb = DataBaseCluster.Get_READ_SERVER("orders", clus_keys);
			List<Object> l = orders.E().Where(orders.note.Equals(note)).Select(_tdb);
			clus_counts2.add(new Clus_count(clus_keys, (long) l.size(), l));
		}

		ArrayList<Object> list = ClusHandle.LimitResult(startidx, endidx, clus_counts2);
		this.WriteUTF8JSONDATAMSG(1, "msg", list);
	}

	/**
	 * 查詢數據
	 */
	public void SelectResultTop() {
		String note = this.getParams("note");
		// int pagesize = 2;
		// String pgidx = this.getParams("pgidx");
		Integer topNum = Integer.parseInt(this.getParams("topNum"));

		// 每个区的满足条件的条数.
		ArrayList<Object> list = new ArrayList<>();

		for (String clus_keys : DataBaseCluster.clusters.get("orders").keySet()) {
			IDatabase _tdb = DataBaseCluster.Get_READ_SERVER("orders", clus_keys);
			List<Object> l = orders.E().Where(orders.note.Equals(note)).PgSize(topNum).Jump(0).Select(_tdb);
			list.addAll(l);
		}

		this.WriteUTF8JSONDATAMSG(1, "msg", list);
	}

}
